﻿require([
"jquery",
"ko",
"bootstrap",
"jquery-cokie",
"jquery-slimscroll",
"akcy"], function ($, ko) {
    var akcy = arguments[arguments.length - 1];
    akcy.ui.init();
    var currentBranche = window.parent.currentuser().branche;
    var currentNode = window.parent.currentuser().node;

    if (currentBranche == "" || currentNode == "") {
        $("#TodayStart").text(0);
        $("#OnTheWay").text(0);
        $("#TodayEntry").text(0);
    } else {
        //akcy.ajaxpost("/ak56api/TransportOrder/StatisticsInfo?branch=" + currentBranche + "&node=" + currentNode, {}).done(function (data) {
        //    var rs = data.Data;
        //    $("#TodayStart").text(rs.TodayStart);
        //    $("#OnTheWay").text(rs.OnTheWay);
        //    $("#TodayEntry").text(rs.TodayEntry);

        //});
    }
    $('#addshortlink').bind('click', function (e) {
        var date = new Date('2015-10-10');
        var year = date.getFullYear();
        date.setDate(year + date.getMonth() + date.getDay() + 7)
        return;
    });
    $.when(
           akcy.ajaxget("/ak56api/service/dictionary?codes=Branches,Nodes")
       ).done(function (dic) {
           var formSearch = akcy.form().options({
               fields: [
                   { field: 'SR_Branch', value: ko.observable(currentBranche) },
                   { field: 'SR_Node', value: ko.observable(currentNode) }
               ]
           }).optionsExtend({
               branches: ko.observableArray(dic["Branches"]),
               nodes: ko.observableArray(dic["Nodes"]),
               refresh: function () {
                   formStatistics.getInstance().sbranches(formSearch.getInstance().SR_Branch());
                   formStatistics.getInstance().snodes(formSearch.getInstance().SR_Node());
                   //formStatistics.load(")
                   //{
                   //    formStatistics.getInstance().snodes(formSearch.getInstance().SR_Node());
                   //};
                   akcy.ajaxpost("/ak56api/DashBoard/GetStatisticsData?branch=" + formSearch.getInstance().SR_Branch() + "&node=" + formSearch.getInstance().SR_Node()).done(function (rst) {
                       if (rst != null) {
                           formStatistics.getInstance().todayIn(rst.todayIn);
                           formStatistics.getInstance().todayUnload(rst.todayUnload);
                           formStatistics.getInstance().whinCount(rst.todayUnload);
                           formStatistics.getInstance().whoutCount(rst.whinCount);
                           formStatistics.getInstance().pendingExp(rst.pendingExp);
                       }
                   });
               }
           }
           ).bind("searchForm");

           var formStatistics = akcy.form().options({
               fields: [
                   { field: 'todayIn', value: ko.observable(0) },
                   { field: 'todayUnload', value: ko.observable(0) },
                   { field: 'whinCount', value: ko.observable(0) },
                   { field: 'whoutCount', value: ko.observable(0) },
                   { field: 'pendingExp', value: ko.observable(0) }
               ]
           }).optionsExtend({
               sbranches: ko.observableArray(dic["Branches"]),
               snodes: ko.observableArray(dic["Nodes"]),
               openDetail: function (vm, context) {
                   var type = $(context.toElement).closest("a").attr("tag");
                   switch (type) {
                       case 'todayIn':
                           akcy.openwindow("今日收货量", "tdcx", true, "/application/TransportOrder/transportorderlist.html?type=S");
                           break;
                       case 'todayUnload':
                           akcy.openwindow("今日落货量", "stdcx", true, "/application/TransportOrder/transportorderlist.html?type=D");
                           break;
                       case 'whCount':
                           akcy.openwindow("库存数量", "kccx", true, "/application/warehouse/warehouse.html");
                           break;
                       case 'pendingExp':
                           akcy.openwindow("待处理异常", "yccx", true, "/application/exception/jobexceptionlist.html");
                           break;
                       default:
                           break;
                   }
               }
           }).bind("statisticsForm");
           formStatistics.load("/ak56api/DashBoard/GetStatisticsData?branch=" + formStatistics.getInstance().sbranches() + "&node=" + formStatistics.getInstance().snodes());

           var noticetable = akcy.table().options({
               fields: [
                   { field: 'branch', value: ko.observable(currentBranche), method: akcy.querymethod.like },
                   { field: 'node', value: ko.observable(currentNode), method: akcy.querymethod.like }
               ],
               headers: [
               ],
               defaultOrderBy: "N_ID",
               dataQueryUrl: '/ak56api/notice/GetNoticeList'
           }).optionsExtend({
               branches: ko.observableArray(dic["BranchesRoot"]),
               nodes: ko.observableArray(dic["Nodes"]),
               getNoticeList: function () {
                   akcy.openwindow("系统通知", 'xxlb', true, "/application/System/noticelist.html?branch=" + currentBranche + "&node=" + currentNode);
               }
           }).bind("noticeList").load();
       });

    //Build shortcuts
    var buildShortCuts = function () {
        if (window.localStorage) {
            var hasSC = true;
            var scHtml = "";
            var curUser = akcy.currentuser.get();
            if (window.localStorage.getItem("SCS") === null) {
                hasSC = false;
            }
            else {
                var objSC = JSON.parse(localStorage.getItem("SCS"));
                if (objSC.hasOwnProperty(curUser)) {
                    for (var idx in objSC[curUser]) {
                        if (idx <= 8) {
                            var parents = "";
                            var menuObj = parent.$('ul.mfixed a[pageid="' + objSC[curUser][idx]["pI"] + '"]');
                            var menuLast = menuObj;
                            if (menuObj.length > 0) {
                                menuObj = menuObj.closest('ul');
                                while (menuObj.hasClass('sub-menu')) {
                                    menuObj = menuObj.closest('li');
                                    parents = menuObj.children('a').children('span').first().html() + (parents == '' ? '' : ' - ' + parents);
                                    menuLast = menuObj.children('a');
                                    menuObj = menuObj.closest('ul');
                                }

                            }
                            if (parents != "") parents = menuLast.children('i')[0].outerHTML + parents;
                            scHtml += '<div class="col-md-4"><a href="javascript:;" class="btn btn-lg blue" pid="' + objSC[curUser][idx]["pI"] + '">' + objSC[curUser][idx]["pT"] + '<div class="mPath">' + parents + '</div></a></div>';
                        }
                        else {
                            break;
                        }
                    }
                }
                else {
                    hasSC = false;
                }
            }

            if (hasSC) $('#scs .portlet-body').html(scHtml + '<div class="clearfix"></div>');
            $('#scs .portlet-body ').off('click', 'a.btn').on('click', 'a.btn', function () {
                parent.$('ul.mfixed a[pageid="' + $(this).attr('pid') + '"]').trigger('click');
            });
        }
    }

    //绑定事件
    $("#run").click(function () {
        lushu.start();
    });
    $("#stop").click(function () {
        lushu.stop();
    });
    $("#pause").click(function () {
        lushu.pause();
    });

    $('#scs a.act-refresh').click(function () { buildShortCuts(); })

    buildShortCuts();
});